home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_023 / ver30 / symbol.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  11KB  |  368 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Symbol table stuff.
  4.  * Version:    29
  5.  * Last edit:    05-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  *
  9.  * Symbol tables, and keymap setup.
  10.  * The terminal specific parts of building the
  11.  * keymap has been moved to a better place.
  12.  */
  13. #include    "def.h"
  14.  
  15. #define    DIRLIST    0            /* Disarmed!            */
  16.  
  17. /*
  18.  * Defined by "main.c".
  19.  */
  20. extern    int    ctrlg();        /* Abort out of things        */
  21. extern    int    quit();            /* Quit                */
  22. extern    int    ctlxlp();        /* Begin macro            */
  23. extern    int    ctlxrp();        /* End macro            */
  24. extern    int    ctlxe();        /* Execute macro        */
  25. extern    int    jeffexit();        /* Jeff Lomicka style exit.    */
  26. extern  int    showversion();        /* Show version numbers, etc.    */
  27.  
  28. /*
  29.  * Defined by "search.c".
  30.  */
  31. extern    int    forwsearch();        /* Search forward        */
  32. extern    int    backsearch();        /* Search backwards        */
  33. extern  int    searchagain();        /* Repeat last search command    */
  34. extern  int    forwisearch();        /* Incremental search forward    */
  35. extern  int    backisearch();        /* Incremental search backwards    */
  36. extern  int    queryrepl();        /* Query replace        */
  37.  
  38. /*
  39.  * Defined by "basic.c".
  40.  */
  41. extern    int    gotobol();        /* Move to start of line    */
  42. extern    int    backchar();        /* Move backward by characters    */
  43. extern    int    gotoeol();        /* Move to end of line        */
  44. extern    int    forwchar();        /* Move forward by characters    */
  45. extern    int    gotobob();        /* Move to start of buffer    */
  46. extern    int    gotoeob();        /* Move to end of buffer    */
  47. extern    int    forwline();        /* Move forward by lines    */
  48. extern    int    backline();        /* Move backward by lines    */
  49. extern    int    forwpage();        /* Move forward by pages    */
  50. extern    int    backpage();        /* Move backward by pages    */
  51. extern    int    setmark();        /* Set mark            */
  52. extern    int    swapmark();        /* Swap "." and mark        */
  53. extern    int    gotoline();        /* Go to a specified line.    */
  54.  
  55. /*
  56.  * Defined by "buffer.c".
  57.  */
  58. extern    int    listbuffers();        /* Display list of buffers    */
  59. extern    int    usebuffer();        /* Switch a window to a buffer    */
  60. extern    int    killbuffer();        /* Make a buffer go away.    */
  61.  
  62. #if    DIRLIST
  63. /*
  64.  * Defined by "dirlist.c".
  65.  */
  66. extern    int    dirlist();        /* Directory list.        */
  67. #endif
  68.  
  69. /*
  70.  * Defined by "display.c".
  71.  */
  72. extern  int    readmsg();        /* Read next line of message.    */
  73.  
  74. /*
  75.  * Defined by "file.c".
  76.  */
  77. extern    int    fileread();        /* Get a file, read only    */
  78. extern    int    filevisit();        /* Get a file, read write    */
  79. extern    int    filewrite();        /* Write a file            */
  80. extern    int    filesave();        /* Save current file        */
  81. extern    int    filename();        /* Adjust file name        */
  82.  
  83. /*
  84.  * Defined by "random.c".
  85.  */
  86. extern    int    selfinsert();        /* Insert character        */
  87. extern    int    showcpos();        /* Show the cursor position    */
  88. extern    int    twiddle();        /* Twiddle characters        */
  89. extern    int    quote();        /* Insert literal        */
  90. extern    int    openline();        /* Open up a blank line        */
  91. extern    int    newline();        /* Insert CR-LF            */
  92. extern    int    deblank();        /* Delete blank lines        */
  93. extern    int    indent();        /* Insert CR-LF, then indent    */
  94. extern    int    forwdel();        /* Forward delete        */
  95. extern    int    backdel();        /* Backward delete        */
  96. extern    int    killline();        /* Kill forward            */
  97. extern    int    yank();            /* Yank back from killbuffer.    */
  98.  
  99. /*
  100.  * Defined by "region.c".
  101.  */
  102. extern    int    killregion();        /* Kill region.            */
  103. extern    int    copyregion();        /* Copy region to kill buffer.    */
  104. extern    int    lowerregion();        /* Lower case region.        */
  105. extern    int    upperregion();        /* Upper case region.        */
  106.  
  107. /*
  108.  * Defined by "spawn.c".
  109.  */
  110. extern    int    spawncli();        /* Run CLI in a subjob.        */
  111.  
  112. /*
  113.  * Defined by "window.c".
  114.  */
  115. extern    int    reposition();        /* Reposition window        */
  116. extern    int    refresh();        /* Refresh the screen        */
  117. extern    int    nextwind();        /* Move to the next window    */
  118. extern  int    prevwind();        /* Move to the previous window    */
  119. extern    int    mvdnwind();        /* Move window down        */
  120. extern    int    mvupwind();        /* Move window up        */
  121. extern    int    onlywind();        /* Make current window only one    */
  122. extern    int    splitwind();        /* Split current window        */
  123. extern    int    enlargewind();        /* Enlarge display window.    */
  124. extern    int    shrinkwind();        /* Shrink window.        */
  125.  
  126. /*
  127.  * Defined by "word.c".
  128.  */
  129. extern    int    backword();        /* Backup by words        */
  130. extern    int    forwword();        /* Advance by words        */
  131. extern    int    upperword();        /* Upper case word.        */
  132. extern    int    lowerword();        /* Lower case word.        */
  133. extern    int    capword();        /* Initial capitalize word.    */
  134. extern    int    delfword();        /* Delete forward word.        */
  135. extern    int    delbword();        /* Delete backward word.    */
  136.  
  137. /*
  138.  * Defined by "extend.c".
  139.  */
  140. extern    int    extend();        /* Extended commands.        */
  141. extern    int    help();            /* Help key.            */
  142. extern    int    bindtokey();        /* Modify key bindings.        */
  143. extern    int    wallchart();        /* Make wall chart.        */
  144.  
  145. typedef    struct    {
  146.     short    k_key;            /* Key to bind.            */
  147.     int    (*k_funcp)();        /* Function.            */
  148.     char    *k_name;        /* Function name string.    */
  149. }    KEY;
  150.  
  151. /*
  152.  * Default key binding table. This contains
  153.  * the function names, the symbol table name, and (possibly)
  154.  * a key binding for the builtin functions. There are no
  155.  * bindings for C-U or C-X. These are done with special
  156.  * code, but should be done normally.
  157.  */
  158. KEY    key[] = {
  159.     KCTRL|'@',    setmark,    "set-mark",
  160.     KCTRL|'A',    gotobol,    "goto-bol",
  161.     KCTRL|'B',    backchar,    "back-char",
  162.     KCTRL|'C',    spawncli,    "spawn-cli",
  163.     KCTRL|'D',    forwdel,    "forw-del-char",
  164.     KCTRL|'E',    gotoeol,    "goto-eol",
  165.     KCTRL|'F',    forwchar,    "forw-char",
  166.     KCTRL|'G',    ctrlg,        "abort",
  167.     KCTRL|'H',    backdel,    "back-del-char",
  168.     KCTRL|'I',    selfinsert,    "ins-self",
  169.     KCTRL|'J',    indent,        "ins-nl-and-indent",
  170.     KCTRL|'K',    killline,    "kill-line",
  171.     KCTRL|'L',    refresh,    "refresh",
  172.     KCTRL|'M',    newline,    "ins-nl",
  173.     KCTRL|'N',    forwline,    "forw-line",
  174.     KCTRL|'O',    openline,    "ins-nl-and-backup",
  175.     KCTRL|'P',    backline,    "back-line",
  176.     KCTRL|'Q',    quote,        "quote",
  177.     KCTRL|'R',    backisearch,    "back-i-search",
  178.     KCTRL|'S',    forwisearch,    "forw-i-search",
  179.     KCTRL|'T',    twiddle,    "twiddle",
  180.     KCTRL|'V',    forwpage,    "forw-page",
  181.     KCTRL|'W',    killregion,    "kill-region",
  182.     KCTRL|'Y',    yank,        "yank",
  183.     KCTRL|'Z',    jeffexit,    "jeff-exit",
  184.     KCTLX|KCTRL|'B',listbuffers,    "display-buffers",
  185.     KCTLX|KCTRL|'C',quit,        "quit",
  186. #if    DIRLIST
  187.     KCTLX|KCTRL|'D',dirlist,    "display-directory",
  188. #endif
  189.     KCTLX|KCTRL|'F',filename,    "set-file-name",
  190.     KCTLX|KCTRL|'L',lowerregion,    "lower-region",
  191.     KCTLX|KCTRL|'N',mvdnwind,    "down-window",
  192.     KCTLX|KCTRL|'O',deblank,    "del-blank-lines",
  193.     KCTLX|KCTRL|'P',mvupwind,    "up-window",
  194.     KCTLX|KCTRL|'R',fileread,    "file-read",
  195.     KCTLX|KCTRL|'S',filesave,    "file-save",
  196.     KCTLX|KCTRL|'U',upperregion,    "upper-region",
  197.     KCTLX|KCTRL|'V',filevisit,    "file-visit",
  198.     KCTLX|KCTRL|'W',filewrite,    "file-write",
  199.     KCTLX|KCTRL|'X',swapmark,    "swap-dot-and-mark",
  200.     KCTLX|KCTRL|'Z',shrinkwind,    "shrink-window",
  201.     KCTLX|'=',    showcpos,    "display-position",
  202.     KCTLX|'(',    ctlxlp,        "start-macro",
  203.     KCTLX|')',    ctlxrp,        "end-macro",
  204.     KCTLX|'1',    onlywind,    "only-window",
  205.     KCTLX|'2',    splitwind,    "split-window",
  206.     KCTLX|'B',    usebuffer,    "use-buffer",
  207.     KCTLX|'E',    ctlxe,        "execute-macro",
  208.     KCTLX|'G',    gotoline,    "goto-line",
  209.     KCTLX|'K',    killbuffer,    "kill-buffer",
  210.     KCTLX|'N',    nextwind,    "forw-window",
  211.     KCTLX|'P',    prevwind,    "back-window",
  212.     KCTLX|'Z',    enlargewind,    "enlarge-window",
  213.     KMETA|KCTRL|'H',delbword,    "back-del-word",
  214.     KMETA|KCTRL|'R',readmsg,    "display-message",
  215.     KMETA|KCTRL|'V',showversion,    "display-version",
  216.     KMETA|'!',    reposition,    "reposition-window",
  217.     KMETA|'>',    gotoeob,    "goto-eob",
  218.     KMETA|'<',    gotobob,    "goto-bob",
  219.     KMETA|'%',    queryrepl,    "query-replace",
  220.     KMETA|'B',    backword,    "back-word",
  221.     KMETA|'C',    capword,    "cap-word",
  222.     KMETA|'D',    delfword,    "forw-del-word",
  223.     KMETA|'F',    forwword,    "forw-word",
  224.     KMETA|'L',    lowerword,    "lower-word",
  225.     KMETA|'R',    backsearch,    "back-search",
  226.     KMETA|'S',    forwsearch,    "forw-search",
  227.     KMETA|'U',    upperword,    "upper-word",
  228.     KMETA|'V',    backpage,    "back-page",
  229.     KMETA|'W',    copyregion,    "copy-region",
  230.     KMETA|'X',    extend,        "extended-command",
  231.     -1,        searchagain,    "search-again",
  232.     -1,        help,        "help",
  233.     -1,        wallchart,    "display-bindings",
  234.     -1,        bindtokey,    "bind-to-key"
  235. };
  236.  
  237. #define    NKEY    (sizeof(key) / sizeof(key[0]))
  238.  
  239. /*
  240.  * Symbol table lookup.
  241.  * Return a pointer to the SYMBOL node, or NULL if
  242.  * the symbol is not found.
  243.  */
  244. SYMBOL    *
  245. symlookup(cp)
  246. register char    *cp;
  247. {
  248.     register SYMBOL    *sp;
  249.  
  250.     sp = symbol[symhash(cp)];
  251.     while (sp != NULL) {
  252.         if (strcmp(cp, sp->s_name) == 0)
  253.             return (sp);
  254.         sp = sp->s_symp;
  255.     }
  256.     return (NULL);
  257. }
  258.  
  259. /*
  260.  * Take a string, and compute the symbol table
  261.  * bucket number. This is done by adding all of the characters
  262.  * together, and taking the sum mod NSHASH. The string probably
  263.  * should not contain any GR characters; if it does the "*cp"
  264.  * may get a nagative number on some machines, and the "%"
  265.  * will return a negative number!
  266.  */
  267. symhash(cp)
  268. register char    *cp;
  269. {
  270.     register int    c;
  271.     register int    n;
  272.  
  273.     n = 0;
  274.     while ((c = *cp++) != 0)
  275.         n += c;
  276.     return (n % NSHASH);
  277. }
  278.  
  279. /*
  280.  * Build initial keymap. The funny keys
  281.  * (commands, odd control characters) are mapped using
  282.  * a big table and calls to "keyadd". The printing characters
  283.  * are done with some do-it-yourself handwaving. The terminal
  284.  * specific keymap initialization code is called at the
  285.  * very end to finish up. All errors are fatal.
  286.  */
  287. keymapinit()
  288. {
  289.     register SYMBOL    *sp;
  290.     register KEY    *kp;
  291.     register int    i;
  292.  
  293.     for (i=0; i<NKEYS; ++i)
  294.         binding[i] = NULL;
  295.     for (kp = &key[0]; kp < &key[NKEY]; ++kp)
  296.         keyadd(kp->k_key, kp->k_funcp, kp->k_name);
  297.     keydup(KCTLX|KCTRL|'G',    "abort");
  298.     keydup(KMETA|KCTRL|'G',    "abort");
  299.     keydup(0x7F,        "back-del-char");
  300.     keydup(KCTLX|'R',    "back-i-search");
  301.     keydup(KCTLX|'S',    "forw-i-search");
  302.     keydup(KMETA|'.',    "set-mark");
  303.     keydup(KMETA|'Q',    "quote");
  304.     keydup(KMETA|0x7F,    "back-del-word");
  305.     /*
  306.      * Should be bound by "tab" already.
  307.      */
  308.     if ((sp=symlookup("ins-self")) == NULL)
  309.         abort();
  310.     for (i=0x20; i<0x7F; ++i) {
  311.         if (binding[i] != NULL)
  312.             abort();
  313.         binding[i] = sp;
  314.         ++sp->s_nkey;
  315.     }
  316.     ttykeymapinit();
  317. }
  318.  
  319. /*
  320.  * Create a new builtin function "name"
  321.  * with function "funcp". If the "new" is a real
  322.  * key, bind it as a side effect. All errors
  323.  * are fatal.
  324.  */
  325. keyadd(new, funcp, name)
  326. int    (*funcp)();
  327. char    *name;
  328. {
  329.     register SYMBOL    *sp;
  330.     register int    hash;
  331.  
  332.     if ((sp=(SYMBOL *)malloc(sizeof(SYMBOL))) == NULL)
  333.         abort();
  334.     hash = symhash(name);
  335.     sp->s_symp = symbol[hash];
  336.     symbol[hash] = sp;
  337.     sp->s_nkey = 0;
  338.     sp->s_name = name;
  339.     sp->s_funcp = funcp;
  340.     if (new >= 0) {                /* Bind this key.    */
  341.         if (binding[new] != NULL)
  342.             abort();
  343.         binding[new] = sp;
  344.         ++sp->s_nkey;
  345.     }
  346. }
  347.  
  348. /*
  349.  * Bind key "new" to the existing
  350.  * routine "name". If the name cannot be found,
  351.  * or the key is already bound, abort.
  352.  */
  353. keydup(new, name)
  354. register int    new;
  355. char        *name;
  356. {
  357.     register SYMBOL    *sp;
  358.  
  359.     if (binding[new]!=NULL || (sp=symlookup(name))==NULL) {
  360.         fprintf (stderr, "keydup: binding[%d] = %x", new, binding[new]);
  361.         fprintf (stderr, " and symlookup(%s) == %x\n", name, sp);
  362.         fflush (stderr);
  363.         abort();
  364.     }
  365.     binding[new] = sp;
  366.     ++sp->s_nkey;
  367. }
  368.